<?php
/**
 * @name 合同控制器
 * @Author : yu
 * @Created : 2016-11-1
 * @Copyright : MPE.Fiidee.Monda.Cn
 */
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\core\Loader;

class ContractAction extends CommonAction
{

    public function C_start()
    {
        parent::C_start();
    }

    protected $serviceBean = "trading.tradingContract.service";


    public function index(HttpRequest $request)
    {
        $contractService = Beans::get($this->serviceBean);
        $orderService = Beans::get('trading.tradingOrder.service');

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page=$page;
        $this->pagesize=$pagesize;

        $search_arr = array(
            "select_mix_option" => $request->getParameter('select_mix_option'),
            "select_mix_option_value" => urldecode($request->getParameter('select_mix_option_value')),
            "order_way" => $request->getParameter('order_way'),
            "status" => $request->getParameter('status'),
            "start_time" => $request->getParameter('start_time'),
            "end_time" => $request->getParameter('end_time'),
        );

        if( $request -> getParameter('export')==1 ){
            $page=1;
            $pagesize =2147483648;
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $tmp = "('0'";
                foreach ($ids as $value){
                    $tmp .= ','."'$value'";
                }
                $search_arr['id']=$tmp.')';
            }
        }
        $arr = $contractService->search($pagesize, $page, $search_arr);

        $items = $arr['items'];

        $itemsLen = count($items);

        for ($i = 0; $i < $itemsLen; $i++) {

            $items[$i]['status'] = $contractService->getStatusText($items[$i]['status']);
            $items[$i]['progress'] = $contractService->getProgressText($items[$i]['progress']);
            $items[$i]['order_way'] = $orderService->getPaywayText2($items[$i]['pay_way']);
        }
        //var_dump($items);
        if( $request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["contract_no"];
                $tmp[] = $v["sellers"];
                $tmp[] = $v["buyers"];
                $tmp[] = $v["order_no"];
                $tmp[] = $v["signed_time"];
                $tmp[] = $v["order_way"];
                $tmp[] = $v["status"];
                $excel[] = $tmp;
            }
            $header = array('合同号', '卖家', '买家', '订单号', '签订时间', '类型', '状态');
            phpexcel("合同列表",$header,$excel);
            exit();
        }


        $this->generatePageMenu($arr['total']);

        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);

        $this->assign("title", "合同列表");
        $this->assign('bread', array('交易管理', $this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
        $this->setView(strtolower($request->getAction()) . '/index');
    }
    public function details(HttpRequest $request)
    {

        $id = $request->getParameter('id');
        $contractModel = Loader::model('tradingContract');

        $item = $contractModel->getItem($id);

        $item ? AjaxResult::ajaxResult('000', '', $item) : AjaxResult::ajaxFailtureResult();
    }

    public function detailsAndProduct(HttpRequest $request)
    {

        $id = $request->getParameter('id');
        $contractModel = Loader::model('tradingContract');
        $item = $contractModel->getItem($id);
        $orderService = Beans::get('trading.tradingOrder.service');

        $order_item = $orderService->getDetail($item['order_id']);
        $orderModel = Loader::model('tradingOrder');
        if(!$order_item){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,'没有此订单');
        }
        if($order_item == 'denied'){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,'您无权限查看此订单');
        }
        if($order_item['order_status'] != $orderModel::ORDER_STATUS_COMPLETED){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,'该订单尚未交易完成');
        }
        $item['shipping_type'] = $order_item['shipping_way']==$orderModel::SHIPPING_WAY_SELLER?1:2;
        $item['pay_type'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?1:2;

        $total_quantity = 0;
        $order_product = array();
        foreach($order_item['product'] as $k=>$v){
            $total_quantity += $v['product_quantity'];
            $attribute = json_decode($v['attribute'],true);
            $order_product[$k]['pkg_size'] = $attribute['pkg_size'];//包装规格
            $order_product[$k]['type_name'] = $attribute['type_name'];//类型
            $order_product[$k]['factory_name'] = $attribute['factory_name'];//厂家
            $order_product[$k]['product_name'] = $attribute['product_name'];//品名
            $order_product[$k]['brand_name'] = $attribute['brand_name'];//牌号
            $order_product[$k]['level'] = $attribute['level'];//质量等级
            $order_product[$k]['product_price'] = $v['product_price'];//单价
            $order_product[$k]['product_quantity'] = $v['product_quantity'];//数量
        }
        unset($order_item['product']);
        unset($order_item['log']);
        unset($order_item['company']);
        $item['product'] = $order_product;
        $item['total_quantity'] = setFloat($total_quantity,3);

        $memberService = Beans::get('member.member.service');
        $member_info = $memberService->getUsernameById($order_item['member_id']);
        $item['username'] = $member_info['username'];

        $item['final_y'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?date('Y',strtotime($item['final_payment_time'])):'';
        $item['final_m'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?date('m',strtotime($item['final_payment_time'])):'';
        $item['final_d'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?date('d',strtotime($item['final_payment_time'])):'';
        $item['shipping_y'] = date('Y',strtotime($order_item['shipping']['shipping_time']));
        $item['shipping_m'] = date('m',strtotime($order_item['shipping']['shipping_time']));
        $item['shipping_d'] = date('d',strtotime($order_item['shipping']['shipping_time']));
        unset($order_item['shipping']);
        foreach($order_item as $k=>$v){
            if($k == 'address'){
                $item['repertory_address'] = $v['address'];
                $item['repertory_name'] = $v['name'];
            }else{
                $item[$k] = $v;
            }
        }

        $item ? AjaxResult::ajaxResult('000', '', $item) : AjaxResult::ajaxFailtureResult();
    }

}
